class h_printer {

	async buildGoodsList(goodsList) {
		let str = ''
		for(let goods of goodsList) {

		}
		return str
	}

	async getTaskList(count = 100) {
		const taskIds = await this.redis.print.scan('print@*', count)
		let taskList = {}
		for(let id of taskIds) {
			taskList[id] = await this.redis.print.get(id)
		}
		return taskList
	}

	async addTask(type, printerCode, orderId, content) {
		const printId = await this.common.createRandomStr(6)
		await this.redis.print.set(`print@${printId}`, {
			printer: printerCode,
			type,
			id: orderId,
			content,
			time: this.common.timestamp()
		}, this.config.taskExpire || 86400)
	}

	async addPrinter(storeId, printerName, printerCode, printerKey, printerPhone = '') {
		const {clientId, clientSecret} = this.config.printer
		const {accessToken} = await this.common.getAccessToken('gtPrinter')
		const timestamp = this.common.timestamp()
		console.log(accessToken)
		const {error, error_description} = await this.common.reqPost({
			url: 'https://open-api.10ss.net/printer/addprinter',
			headers: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				client_id: clientId,
				machine_code: printerCode,
				msign: printerKey,
				access_token: accessToken,
				id: this.common.uuid4(),
				timestamp,
				phone: printerPhone,
				print_name: printerName,
				sign: this.common.md5(clientId + timestamp + clientSecret)
			}
		})
		if(error != '0') {
			console.error(error, error_description)
			return this.reject('-2033', 'add printer to elyun failed')
		}
		const printerId = await this.common.createRandomStr(10)
		const template = await this.mysql.common.select('printer_template', 'templateId', 'id=0')
		if(!template[0]) 
			return this.reject('-2034', 'print template not found')
		const printerTemplate = template[0].templateId
		if(!await this.mysql.common.insert('store_printer', 'printerId, printerName, printerCode, printerKey, printerTemplate, printerPhone, printerStore', [printerId, printerName, printerCode, printerKey, printerTemplate, printerPhone, storeId]))
			return this.reject('-2035', 'add printer failed')
		return printerId
	}

	async removePrinter(printerId) {

	}

	async updatePrinterStatus(printerCode, printerStatus) {
		await this.mysql.common.update('store_printer', 'printerStatus=?', 'printerCode=?', [printerStatus, printerCode])
	}

	async checkElyunSign(timestamp, sign) {
		const {clientId, clientSecret} = this.config.printer
		if(sign != this.common.md5(clientId + timestamp + clientSecret))
			return this.reject('-2042', 'elyun push data invalid')
	}

}

module.exports = h_printer